<?php
 jimport('joomla.application.component.view');
class svViewChucdanh extends JView
{
	function display()
	{
		$task = JRequest::getCMD('task');
		switch ($task)
		{
			case 'add':
				$this->_add();
				break;
			case 'edit':
				$this->_edit();
				break;
			case 'save':
				$this->_save();
				break;
			case 'cancel':
				$this->_cancel();
				break;
			case 'remove':
				$this->_remove();
				break;
			default :
				$this->_default();
				break;
		}
		parent::display();
	}
	// hien thi chi tiet
	function _default()
	{
		// them toolbar
		$this->_gridToolbar();
		// ket noi voi model
		$model=$this->getModel('chucdanh');
		// cac bien gia tri
		$result=$model->getAllChucdanh();
		$title='Quản lý các chức danh';
		// set layout
		$this->assignRef('dataObject', $result);
		$this->assignRef('title', $title);
		$this->setLayout('default');
	}
	function _add()
	{
		$this->_formToolbar('Thêm mới một chức danh');
		// chon cac donviquanly
		$model=$this->getModel('chucdanh'); 
		// set layout
		$this->setLayout('add');
	}
	function _edit()
	{
		// toolbar
		$this->_formToolbar('Chỉnh sửa chức danh');
		// lay id tu form default
		$idlist    = JRequest::getVar('cid',array(0),'','array');
		JArrayHelper::toInteger($idlist, array(0));
		$idchucdanh     = $idlist[0];
		// lay du lieu tu model
		$model=$this->getModel('chucdanh');
		$chucdanh=$model->getChucdanhByID($idchucdanh);
		// set layout
		$this->assignRef('chucdanh', $chucdanh);
		$this->setLayout('edit');
	}
	function _save()
	{
		// lay du lieu
		$ten=JRequest::getVar('ten');
		$mieuta = JRequest::getVar('mieuta', 'Mieu ta ve phong ban', 'POST','validation type',JREQUEST_ALLOWHTML);
		// xu ly yeu cau edit/insert
		$model=$this->getModel('chucdanh');
		$idchucdanh=JRequest::getInt('idchucdanh');
		if($idchucdanh==0)// xac thuc viec them mot item
		{
			$exec=$model->addNewChucdanh($ten,$mieuta);
		}
		else//xac thuc viec edit mot item
		{
			$exec=$model->editChucdanh($idchucdanh,$ten,$mieuta);
		}
		echo $exec;
		$this->_default();
	}
	function _cancel()
	{
		header('Location:index.php?option=com_svquanlycanbo&view=chucdanh');
	}
	function _remove()
	{
		// lay id tu form default
		$idlist    = JRequest::getVar('cid',array(0),'','array');
		JArrayHelper::toInteger($idlist, array(0));
		$idchucvu    = $idlist[0];
		// thuc hien yeu cau remove
		$model=$this->getModel('chucdanh');
		$exec=$model->delChucvu($idchucvu);
		echo $exec;
		$this->_default();
		
	}
	// them cac button tren thanh toolbar
	function _gridToolbar()
	{
		JToolBarHelper::title('Quản lý các chức danh.!');
		JToolBarHelper::addNew();
		JToolBarHelper::editListX();
		JToolBarHelper::deleteList();
	}
	function _formToolbar($title)
	{
		JToolBarHelper::title($title);
		JToolBarHelper::save();
		JToolBarHelper::cancel();
	}
	
}
?>